package com.weather.common.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: RabbitConfig
 * @Author: yf.zhu
 * @CreateDate: 2020/8/15 19:59
 * @Version: 1.0
 */
@Configuration
public class RabbitConfig {

    /**
     * 消费者数量，默认10
     */
    public static final int DEFAULT_CONCURRENT = 10;

    /**
     * 每个消费者获取最大投递数量 默认50
     */
    public static final int DEFAULT_PREFETCH_COUNT = 50;

    /**
     * TOPICWEATHER
     */
    public final static String TOPICWEATHER = "topic.weather";

    /**
     * TOPICWEATHER
     */
    public final static String TOPICWEATHERDIE = "topic.weatherdie";


    /**
     * TOPICWEATHERALL
     */
//    public final static String TOPICWEATHERALL = "topic.weatherall";

    /**
     * 交换机名称
     */
    public final static String TOPICEXCHANGE = "TopicExchange";

    /**
     * TOPICWEATHER
     */
    public final static String ROUTING_WEATHERKEY = "topic.weatherkey";

    /**
     * WEATHERDIEKEY
     */
    public final static String ROUTING_WEATHERDIEKEY = "topic.weatherdiekey";
    /**
     * TOPICWEATHERALL
     */
//    public final static String ROUTING_WEATHERALLKEY = "topic.#";

    @Bean
    public Queue topicWeatherDie() {
        return new Queue(TOPICWEATHERDIE,true);
    }

    /**
     * topic模式交换机队列
     * @return
     */
    @Bean
    public Queue topicWeather() {
        return new Queue(TOPICWEATHER,true);
    }

    /**
     * topic模式交换机队列
     * @return
     */
//    @Bean
//    public Queue topicWeatherAll() {
//        return new Queue(TOPICWEATHERALL,true);
//    }

    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(TOPICEXCHANGE);
    }

    @Bean
    public Binding topicBingingWeatherDie(){
        return BindingBuilder.bind(topicWeatherDie()).to(topicExchange()).with(ROUTING_WEATHERDIEKEY);
    }

    @Bean
    public Binding topicBingingWeather(){
        return BindingBuilder.bind(topicWeather()).to(topicExchange()).with(ROUTING_WEATHERKEY);
    }

//    @Bean
//    public Binding topicBingingWeatherALL(){
//        return BindingBuilder.bind(topicWeatherAll()).to(topicExchange()).with(ROUTING_WEATHERALLKEY);
//    }

    /**
     * 并发消费配置
     * @param configurer
     * @param connectionFactory
     * @return
     */
    @Bean("pointTaskContainerFactory")
    public SimpleRabbitListenerContainerFactory pointTaskContainerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setPrefetchCount(DEFAULT_PREFETCH_COUNT);
        factory.setConcurrentConsumers(DEFAULT_CONCURRENT);
        configurer.configure(factory, connectionFactory);
        return factory;
    }



}